package nz.ac.massey.spidernetpn.database;

import java.beans.XMLEncoder;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.commons.collections15.Factory;
import org.xml.sax.SAXException;

import com.sun.xml.internal.stream.writers.XMLWriter;

import nz.ac.massey.spidernetpn.graph.*;
import edu.uci.ics.jung.graph.DirectedGraph;
import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.io.GraphFile;
import edu.uci.ics.jung.io.GraphMLReader;
import edu.uci.ics.jung.io.GraphMLWriter;

public class GraphDatabase implements GraphDatabaseInterface  {

//	public ArrayList getListOfStoredGraphsOLD() {
//		ArrayList listOfGraphs = new ArrayList();
//		listOfGraphs.add("Graph of www.Google.com");
//		listOfGraphs.add("Graph of www.massey.ac.nz");
//		return listOfGraphs;
//	}
	
	public List getListOfStoredGraphs() {
		String workingDirectory = "test\\Graphs\\";
		
		List listOfGraphs = new ArrayList<String>();
		
		FileReader fr = null;
	
		File dir = new File(workingDirectory);

		FilenameFilter only = new OnlyExt("xml"); 
		String[] children = dir.list(only); 
		
	    if (children == null) {
	        // Either dir does not exist or is not a directory
	    } else {
	        for (int i=0; i<children.length; i++) {
	            // Get filename of file or directory
	            String filename = children[i];
	           // System.out.println("File "+i+" "+filename);
	            listOfGraphs.add(filename);
	        }
	    }
			
		return listOfGraphs;
	
	}

	public boolean saveGraphOLD(String name, DirectedGraph<URL, Integer> graph)
			throws Exception {
		FileOutputStream fileStream = null;
		try {
			fileStream = new FileOutputStream("test\\Graphs\\Test.xml");
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
		
		XMLEncoder e = new XMLEncoder( new BufferedOutputStream(fileStream));
		System.out.println("Graph"+graph.toString());
		e.writeObject(graph);
	
		e.close();

		return true;
	}

	public boolean saveGraph(String name, DirectedGraph<URL, Integer> graph){
		
		GraphMLWriter<URL, Integer> gWriter = new GraphMLWriter<URL, Integer>();
		
		Writer out = null;
		FileOutputStream fos;
		try {
			fos = new FileOutputStream("test\\Graphs\\"+name+".xml");
			out = new OutputStreamWriter(fos, "UTF8");
			gWriter.save(graph, out);
			out.close();
		} catch (Exception e1) {
			e1.printStackTrace();
			return false;
		}
	
		return true;
	}
	
	public DirectedGraph<URL, Integer> loadCustomGraph(String name)
	throws IOException {
		
		DirectedGraph<URL, Integer> testGraph;
		testGraph = null;
		//System.out.println("In setup");
		URL rootNode = new URL("http://www.massey.ac.nz");
		URL node1 = new URL("http://www.massey.ac.nz");
		URL node2 = new URL("https://alumnionline.massey.ac.nz/");
		URL node3 = new URL("https://secure.mymassey.com/");
		
		URL node4 = new URL("http://alumnionline.massey.ac.nz/NetCommunity/Page.aspx?pid=334");
		URL node5 = new URL("http://masseynews/massey/about-us/news/print-publications/definingnz.cfm");
		
		testGraph = new DirectedSparseMultigraph<URL, Integer>();
		
		//loadedGraph.addVertex(rootNode);
		testGraph.addEdge(1, rootNode, node1);

		testGraph.addEdge(2, rootNode, node2);
		testGraph.addEdge(3, rootNode, node3);
		testGraph.addEdge(4, node2, node4);
		testGraph.addEdge(5, node2, node5);
		return testGraph;
	
	}
	
	public DirectedGraph<URL, Integer> loadCustomGraphNEW(String name)
			throws IOException {
		
		String  fullPath = "C:\\Users\\Daniel\\Desktop\\Software Engineering C\\Project Workspace\\spidernetpn\\";
		String src = fullPath+"test\\Graphs\\"+name+".xml";
		Reader reader = new InputStreamReader(new FileInputStream(src),"UTF8"); 
//		
		GraphMLReader gReader = null;
		try {
			gReader = new GraphMLReader();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		DirectedGraph<URL, Integer> loadedGraph = new DirectedSparseMultigraph<URL, Integer>();
		
		gReader.load(reader, loadedGraph);
		
//		Factory<Graph<Number, Number>> graphFactory;
//		Factory<Number> vertexFactory;
//		Factory<Number> edgeFactory;
//		GraphMLReader<Graph<Number, Number>, Number, Number> gmlreader;
//
//		graphFactory = new Factory<Graph<Number, Number>>()
//		{
//			public Graph<Number, Number> create()
//		    {
//				return new DirectedSparseMultigraph<Number, Number>();
//		    }
//		};
//		          
//		vertexFactory = new Factory<Number>()
//		{
//			int n = 0;
//		  
//		    public Number create()
//		    {
//		    	return n++;
//		    }
//		};
//		              
//		edgeFactory = new Factory<Number>()
//		{
//			int n = 0;
//		 
//		    public Number create()
//		    {
//		    	return n++;
//		    }
//		 };
		 
		// gmlreader = new GraphMLReader<Graph<Number, Number>, Number, Number>(vertexFactory, edgeFactory);

		//System.out.println("got here??"+src);
		// DirectedGraph<URL, Integer> g = loadGraph(src);
		
		
//		DirectedGraph<URL, Integer> loadedGraph = new DirectedSparseMultigraph<URL, Integer>();
		
		//gmlreader.load(src, loadedGraph);
		//GraphMLReader greader = new GraphMLReader(reader);        
		// DirectedGraph<Vertex, Edge> g = greader.readGraph(); 
		//Graph<Number, Number> graph = loadGraph(src);

		
		 
		//System.out.println("got here??"+src);
		//gReader.load(src, loadedGraph);
		//System.out.println("got here"+loadedGraph);		
		//System.out.println(loadedGraph);
		return loadedGraph;
	}

	private static class OnlyExt implements FilenameFilter { 
		String ext; 
		public OnlyExt(String ext) { 
				this.ext = "." + ext; 
			} 
			public boolean accept(File dir, String name) { 
				return name.endsWith(ext); 
			} 
		}
	
}
